<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;

/**
 * 商品
 *
 * @icon fa fa-circle-o
 */
class Goods extends Backend
{

    /**
     * Goods模型对象
     * @var \app\admin\model\Goods
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Goods;
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign("isExplosiveList", $this->model->getIsExplosiveList());
        $this->view->assign("isOverbalanceList", $this->model->getIsOverbalanceList());
        $this->view->assign("isRecommendList", $this->model->getIsRecommendList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    public function del($ids = null)
    {
        if (false === $this->request->isPost()) {
            $this->error(__("Invalid parameters"));
        }
        $ids = $ids ?: $this->request->post("ids");
        if (empty($ids)) {
            $this->error(__('Parameter %s can not be empty', 'ids'));
        }
        $pk = $this->model->getPk();
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            $this->model->where($this->dataLimitField, 'in', $adminIds);
        }
        $list = $this->model->where($pk, 'in', $ids)->select();

        $count = 0;
        Db::startTrans();
        try {
            foreach ($list as $item) {
                $count += $item->delete();
            }
            db('explosive_goods')->whereIn('goods_id',$ids)->delete();
            db('overbalance_goods')->whereIn('goods_id',$ids)->delete();
            db('recommend_goods')->whereIn('goods_id',$ids)->delete();
            Db::commit();
        } catch (PDOException|\Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if ($count) {
            $this->success();
        }
        $this->error(__('No rows were deleted'));
    }
    public function up()
    {
        //上架
        $ids = input('ids');
        $row = $this->model->get(['id' => $ids]);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        if (false === $this->request->isPost()) {
            $this->view->assign('row', $row);
            return $this->view->fetch();
        }
        $params['status'] = 1;
        $result = false;
        Db::startTrans();
        try {
            //是否采用模型验证
            $result = $row->allowField(true)->save($params);
            db('explosive_goods')->where(['goods_id'=>$ids])->update([
                'status' => 1
            ]);
            db('overbalance_goods')->where(['goods_id'=>$ids])->update([
                'status' => 1
            ]);
            db('recommend_goods')->where(['goods_id'=>$ids])->update([
                'status' => 1
            ]);
            Db::commit();
        } catch (ValidateException|PDOException|\Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if (false === $result) {
            $this->error(__('No rows were updated'));
        }
        $this->success();
    }
    public function down()
    {
        //下架
        $ids = input('ids');
        $row = $this->model->get(['id' => $ids]);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        if (false === $this->request->isPost()) {
            $this->view->assign('row', $row);
            return $this->view->fetch();
        }
        $params['status'] = 2;
        $result = false;
        Db::startTrans();
        try {
            $result = $row->allowField(true)->save($params);
            db('explosive_goods')->where(['goods_id'=>$ids])->update([
                'status' => 2
            ]);
            db('overbalance_goods')->where(['goods_id'=>$ids])->update([
                'status' => 2
            ]);
            db('recommend_goods')->where(['goods_id'=>$ids])->update([
                'status' => 2
            ]);
            Db::commit();
        } catch (ValidateException|PDOException|\Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if (false === $result) {
            $this->error(__('No rows were updated'));
        }
        $this->success();
    }
}
